package dataStructures.list;

import java.util.Iterator;

/**
 *
 * A simple linked list that only allows append and iterate. Useful for
 * situations in which the user is only adding to a list and iterating over
 * the list at a later time without consideration of order or requirement of
 * other operations, such as remove. When iterating the list, elements are
 * returned in the same way a queue would (first in, first out).
 *
 * Fully Tested: No
 * Fully Documented: Yes
 * Created: 19-Nov-2011
 * Last Updated: 19-Nov-2011
 *
 * @author Shimu Wu
 */
public final class SimpleList<T> implements Iterable<T> {

    private int size;

    private SinglelyLinkedNode<T> first, last;

    /**
     * Instantiates a SimpleList containing only the given firstData.
     * @param firstData the firstData to be stored in the SimpleList
     */
    public SimpleList(T firstData) {
        this(new SinglelyLinkedNode<T>(firstData));
    }

    public SimpleList(SinglelyLinkedNode<T> first) {
        size = 0;
        this.last = first;
        this.first = last;
    }

    /**
     * Add the given data into this SimpleList
     * 
     * @param data the data to be added to this list
     */
    public final void add(T data) {
        SinglelyLinkedNode<T> next = this.last;
        this.last = new SinglelyLinkedNode<T>(data, next);
        size++;
    }

    /**
     * Joins the given SimpleList with this SimpleList
     * @param simpleList
     */
    public final void join(SimpleList<T> simpleList) {
        this.last.next = simpleList.first;
    }

    public Iterator<T> iterator() {
        class SimpleListIterator implements Iterator<T> {

            private SinglelyLinkedNode<T> current;

            private SimpleListIterator() {
                this.current = first;
            }

            public boolean hasNext() {
                return (current != null);
            }

            public T next() {
                T returnData = current.data;
                current = current.next;
                return returnData;
            }

            public void remove() {
            }
        }
        return new SimpleListIterator();
    }
}
